<template>
  <el-dialog
    :visible.sync="dep2CreateDialog"
    :close-on-click-modal="false"
    :title="depCreateTitle"
    :before-close="depCreateClose"
    width="30%"
  >
    <el-form
      ref="depForm"
      :model="depForm"
      :rules="depRules"
      label-width="80px"
    >
      <el-form-item label="部门名称" prop="depCreateLabelValue">
        <el-input
          v-model="depForm.depCreateLabelValue"
          :maxlength="20"
          clearable
          placeholder="请输入内容"
        />
      </el-form-item>
      <el-form-item
        label="上级部门"
        prop="depSelect"
        v-if="depForm.depSelect !== 0"
      >
        <DepSelect
          @change="selectChange"
          :value="depForm.depSelect"
          style="width:100%"
          v-if="dep2CreateDialog"
        ></DepSelect>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="depCreateClose">取 消</el-button>
      <el-button type="primary" @click="submitDialog">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
/**
 * 新增部门
 */
import DepSelect from "../../components/DepSelect"; //部门选择器-单选

export default {
  model: {
    prop: "depForm",
    event: "change",
  },
  components: {
    DepSelect,
  },
  data() {
    return {
      dep2CreateDialog: false,
      depRules: {
        depSelect: [
          { required: true, message: "上级部门不能为空", trigger: "blur" },
        ],
        depCreateLabelValue: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  props: ["depCreateDialog", "depCreateTitle", "superDepList", "depForm"],
  watch: {
    depCreateDialog(val) {
      if (val) {
        this.dep2CreateDialog = true;
      } else {
        this.dep2CreateDialog = false;
      }
    },
  },
  methods: {
    // 部门-单选-确定
    selectChange(data) {
      this.$set(this.depForm, "depSelect", data.deptId);
      // this.depName = data.name;
    },
    /**
     * 关闭弹框
     */
    depCreateClose() {
      this.resetDep();
      this.dep2CreateDialog = false;
    },
    resetDep() {
      this.$refs.depForm.resetFields();
    },
    /**
     * 提交弹框
     */
    submitDialog() {
      this.$refs.depForm.validate((valid) => {
        if (valid) {
          this.$emit("submitDialog");
        }
      });
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.label,
        children: node.children,
      };
    },
  },
};
</script>